/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.service.impl;

import com.darren.euc.entity.Dept;
import com.darren.euc.entity.DeptVO;
import com.darren.euc.repository.DeptRepository;
import com.darren.euc.service.DeptService;
import com.darren.euc.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/17
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptRepository deptRepository;

    public List<Dept> findByClientId(Integer clientId){
        // 不包含顶级部门
        return deptRepository.findByClientIdAndParentIdNotAndStatus(clientId,0,Constant.NORMAL);
    }

    @Override
    public Dept addDept(DeptVO dept) {
        Dept realDept = dept.toDept();
        return deptRepository.save(realDept);
    }

    @Override
    public Boolean deleteDeptById(int id) throws Exception {
        Dept dept = deptRepository.findOne(id);
        if(dept == null){
            throw new Exception("不存在的部门");
        }

        dept.setStatus(Constant.DELETE);
        return deptRepository.save(dept)!=null?true:false;
    }

    @Override
    public List<Dept> getAll(Integer clientId) {
        return deptRepository.findByClientIdAndStatus(clientId, Constant.NORMAL);
    }
}
